﻿function TemplateReplace(TemplateText, entity, ResultDict)
{
	var tempTexts = new Array();
	if (typeof ResultDict != "undefined" && ResultDict != null)
	{
		var boolRegex = new RegExp("\\{\\S+\\?.*?\\|.*?\\}", "g");  //三元运算{bool?value1|value2}
		do
		{
			var startIndex = boolRegex.lastIndex;
			var groups = boolRegex.exec(TemplateText);
			if (groups == null)
			{
				tempTexts[tempTexts.length] = TemplateText.substring(startIndex, TemplateText.length);
				break;
			}
			var valueOld = groups[0];
			tempTexts[tempTexts.length] = TemplateText.substring(startIndex, boolRegex.lastIndex - valueOld.length);
			var parttexts = groups[0].substr(1, groups[0].length - 2).split('?');
			var fieldName = parttexts[0];
			var value = "";
			if (typeof ResultDict[fieldName] != "undefined" && ResultDict[fieldName])
			{
				value = parttexts[1].split('|')[0];
			}
			else
			{
				value = parttexts[1].split('|')[1];
			}
			tempTexts[tempTexts.length] = value;
		} while (true);
		if (tempTexts.length != 0) TemplateText = tempTexts.join("");
	}

	var fieldRegex = new RegExp("\\[\\w+(\\.\\w+(\\(.*?\\))?)?\\]", "g");
	var selectBlockRegex = new RegExp("\\<:.*?\\[(\\w+)(\\.\\w+(\\(.*?\\))?)?\\].*?\\>", "g");

	tempTexts = new Array();
	do
	{
		var startIndex = selectBlockRegex.lastIndex;
		var groupsLong = selectBlockRegex.exec(TemplateText);
		if (groupsLong == null)
		{
			tempTexts[tempTexts.length] = TemplateText.substring(startIndex, TemplateText.length);
			break;
		}
		var valueOld = groupsLong[0];
		tempTexts[tempTexts.length] = TemplateText.substring(startIndex, selectBlockRegex.lastIndex - valueOld.length);
		var valueNew = "";
		var fieldName = groupsLong[1];
		if (typeof entity[fieldName] != "undefined" && entity[fieldName] != false)
		{
			var lastIndex = fieldRegex.lastIndex;
			fieldRegex.lastIndex = 0;
			var groups = fieldRegex.exec(valueOld);
			valueNew = formatValue(groups, entity);
			tempTexts[tempTexts.length] = valueOld.substring(2, valueOld.indexOf("[" + fieldName)) + valueNew + valueOld.substring(fieldRegex.lastIndex, valueOld.length - 1);
		}
	} while (true);
	if (tempTexts.length != 0) TemplateText = tempTexts.join("");

	//var fieldRegex2 = new RegExp("\\[\\w+\\]|\\[\\w+\\.\\w+\\]|\\[\\w+\\.\\w+\\(.*\\)\\]", "g");
	tempTexts = new Array();
	fieldRegex.lastIndex = 0;
	do
	{
		var startIndex = fieldRegex.lastIndex;
		var groups = fieldRegex.exec(TemplateText);
		if (groups == null)
		{
			tempTexts[tempTexts.length] = TemplateText.substring(startIndex, TemplateText.length);
			break;
		}
		var valueOld = groups[0];
		tempTexts[tempTexts.length] = TemplateText.substring(startIndex, fieldRegex.lastIndex - valueOld.length);
		var valueNew = formatValue(groups, entity);
		tempTexts[tempTexts.length] = valueNew;
	} while (true);
	if (tempTexts.length != 0) TemplateText = tempTexts.join("");

	return TemplateText;
}

function formatValue(groups, entity)
{
	var value = "";
	var matchedString = groups[0];
	var fieldName = matchedString.substr(1, matchedString.length - 2).split('.')[0];
	var fieldValue = typeof entity[fieldName] == "undefined" ? "" : entity[fieldName];
	if (typeof groups[1] != "undefined" && groups[1] != null && groups[1] != "")
	{
		if (typeof groups[2] != "undefined" && groups[2] != null && groups[2] != "")
		{
			var strs = groups[1].split(new RegExp("\\.|\\(|\\)", "g"));
			var methodName = strs[0];
			var paras = strs[1].split(',');
			switch (methodName)
			{
				case "lenCut":
					{
						var len = fieldValue.toString().length;
						len = isNaN(parseInt(paras[0], 0)) ? len : parseInt(paras[0], 0);
						if (len > 0 && len < fieldValue.toString().length)
						{
							value = "<span title=\"" + fieldValue.toString().HtmlEncode() + "\">" + fieldValue.toString().substr(0, len) + "...</span>";
						}
						else
						{
							value = fieldValue.toString().HtmlEncode();
						}
					}
					break;
				default:
					value = fieldValue.toString().HtmlEncode();
					break;
			}
		}
		else
		{
			var property = groups[1].substr(1, groups[1].length - 1);
			switch (property)
			{
				case "Date":
					{
						if (fieldValue instanceof Date)
						{
							value = fieldValue.toLocaleDateString();
						}
						else
						{
							value = fieldValue.toString().HtmlEncode();
						}
					}
					break;
				case "Time":
					{
						if (fieldValue instanceof Date)
						{
							value = fieldValue.toLocaleTimeString();
						}
						else
						{
							value = fieldValue.toString().HtmlEncode();
						}
					}
					break;
				default:
					value = fieldValue.toString().HtmlEncode();
					break;
			}
		}
	}
	else
	{
		value = fieldValue == null ? "" : fieldValue.toString().HtmlEncode();
	}
	return value;
}


//var RegexValidator = function (RegexText)
//{
//	return function ()
//	{
//		return new RegExp(RegexText, "g").test(this.SourceText);
//	}
//}

String.prototype.HtmlEncode = function ()
{
	var text = this.toString();
	var div = document.createElement('div');
	if (typeof div.innerText != "undefined")
	{
		div.innerText = text;
	} else
	{
		div.textContent = text;
	}
	var strHTML = div.innerHTML;
	div = null;
	return strHTML;
};

String.prototype.HtmlDecode = function ()
{
	var strHTML = this.toString();
	var div = document.createElement('div');
	div.innerHTML = strHTML;
	var text = "";
	if (typeof div.innerText != "undefined")
	{
		text = div.innerText;
	} else
	{
		text = div.textContent;
	}
	div = null;
	return text;
};